﻿using UnityEngine;

namespace Core
{
    /// <summary>
    /// 圆形范围发射子弹buff
    /// </summary>
    [Buff]
    public class CreateBulletBuff : BuffSystem
    {
        protected override void OnAwake()
        {
            int.TryParse(config.param1, out int startAngle);
            int.TryParse(config.param2, out int angle);
            int.TryParse(config.param3, out int bulletId);
            Vector3 offestPosition = (Vector3)ConvertHelper.ChangeType(config.param4, typeof(Vector3));

            float totalAngle = 360f;
            if(!string.IsNullOrEmpty(config.param5))
                float.TryParse(config.param5, out totalAngle);

            // 总角度除以每个子弹角度得到需要发射子弹数量
            int count = (int)(totalAngle / angle);

            var position = target.Position;

            for (int i = 0; i < count; i++)
            {
                var bulletAngle = angle * i + startAngle;
                var bullet = World.GetComponent<BulletComponent>().CreateBullet(target, bulletId);
                var quaternion = Quaternion.AngleAxis(bulletAngle, Vector3.up);
                var startPosition = quaternion * offestPosition;
                bullet.Launch(position + startPosition, (startPosition - position).normalized);
            }

            this.Dispose();
        }
    }

}